package cn.com.blackboard.SISIntergrationBiz.controller;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import blackboard.base.BaseComparator;
import blackboard.base.GenericFieldComparator;
import blackboard.platform.BbServiceManager;
import cn.com.blackboard.DataModule4B2.BBDataUtils.LocaleUtil;
import cn.com.blackboard.DataModule4B2.MemoryCache.CourseInfoMemCache;
import cn.com.blackboard.DataModule4B2.model.CourseInfo;
import cn.com.blackboard.DataModule4B2.service.CourseService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
  
@Controller
@RequestMapping("/course")
public class CourseInfoController {  
	
	private static final Logger logger = LoggerFactory.getLogger(CourseInfoController.class);

    @Autowired  
    private CourseService courseService;
    
    @RequestMapping("/getCourseBycondition")  
    public String getUserByCondition(@RequestParam(value = "condition",required=false) String conditionJsonStr,
									    		ModelMap modelMap
									    		) throws IOException{
    	logger.info("getCourseBycondition/jsonStr"+conditionJsonStr);

    	
    	List<CourseInfo> list = courseService.getCourse(conditionJsonStr);
		GenericFieldComparator courseidComparator = new GenericFieldComparator( BaseComparator.ASCENDING, "getCourseId", CourseInfo.class );
		GenericFieldComparator getCourseNameComparator = new GenericFieldComparator( BaseComparator.ASCENDING, "getCourseName", CourseInfo.class );
		GenericFieldComparator getDepartmentIdComparator = new GenericFieldComparator( BaseComparator.ASCENDING, "getDepartmentId", CourseInfo.class );
		
		modelMap.addAttribute("courseidComparator", courseidComparator);
		modelMap.addAttribute("getCourseNameComparator", getCourseNameComparator);
		modelMap.addAttribute("getDepartmentIdComparator", getDepartmentIdComparator);
		modelMap.addAttribute("list", list);
		Locale locale = LocaleUtil.getLocale();
		modelMap.addAttribute("locale", locale.toString());
		return "/import/importCourse"; 
    }
    
    @RequestMapping(value = "importCourse", method = {RequestMethod.POST },produces={"text/html;charset=UTF-8;","application/json;"}) 
    @ResponseBody  
    public String importCourse(@RequestBody List<String> keyList
									    		) throws IOException{
    	
    	logger.info("into import course");
    	logger.info("listsize = "+keyList.size());
    	List<CourseInfo> list = CourseInfoMemCache.getListByKeyList(keyList);
    	String result = "";
    	List<BatchResultModel> resultModelList = courseService.importCourse(list);
    	for(BatchResultModel rsModel:resultModelList){
    		result+=rsModel.getModelClsName()+"."+rsModel.getModelClsAttr()+":"+rsModel.getValue()+"-->"+rsModel.getStatus().getValue()+"\n";
    	}
		return result;
    }

    @RequestMapping(value = "batchImportCourse", method = {RequestMethod.POST },produces={"text/html;charset=UTF-8;","application/json;"}) 
    @ResponseBody  
    public String batchImportCourse() throws IOException{
    	
    	logger.info("into batch import course");
    	String result = "";
    	List<BatchResultModel> resultModelList = courseService.batchImportCourse();
    	for(BatchResultModel rsModel:resultModelList){
    		result+=rsModel.getModelClsName()+"."+rsModel.getModelClsAttr()+":"+rsModel.getValue()+"-->"+rsModel.getStatus().getValue()+"\n";
    	}
		return result;
    }
}  
